require "import"
import "tools"
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS).setStatusBarColor(0xff4DB6AC)
activity.setContentView("avAddAudio_")
oDir.text=LUA_SD.."/avHandle/output/"
iDir.onClick=function()
  ChoiceFile(LUA_SD,function(dir)
    iDir.text=dir
    dialogPlayerVideo(activity,dir)
  end)
end
oDir.onClick=function()
  ChoicePath(LUA_SD,function(dir)
    oDir.text=dir.."/"
  end)
end
item=
{
  LinearLayout;
  gravity="center|left";
  orientation="vertical";
  layout_height="50dp";
  layout_width="-1";
  {
    TextView;
    layout_marginLeft="20dp";
    text="路径";
    id="dir";
  };
  {
    LinearLayout;
    layout_marginLeft="20dp";
    {
      TextView;
      text="延时";
      id="tip";
    };
    {
      TextView;
      layout_marginLeft="10dp";
      id="delay";
    };
  };
};

adapter=LuaAdapter(activity,item)
adapter.add{dir="点击添加音频文件",tip=""}
lv.Adapter=adapter
lv.onItemClick=function(a,b,c,d)
  local s= b.Tag.dir.text
  if s=="点击添加音频文件"
    ChoiceFile(LUA_SD,function(p)
      table.insert(adapter.data,#adapter.data,{dir=p,tip="延时",delay="0秒"})
      adapter.notifyDataSetChanged()
    end)
   else
    local items={"删除","延时","预览","上移","下移"}
    AlertDialog.Builder(this) .setItems(items,{onClick=function(l,v)
        local str=items[v+1]
        if str=="删除"
          table.remove(adapter.data,d)
          adapter.notifyDataSetChanged()
        end
        if str=="预览"
          dialogPlayer(s)
        end
        if str=="上移"
          local data =adapter.data[d]
          if d-1>0
            table.remove(adapter.data,d)
            table.insert(adapter.data,d-1,data)
            adapter.notifyDataSetChanged()
           else
            print("最上边了")
          end
        end
        if str=="下移"
          local data =adapter.data[d]
          if d+2<=#adapter.data
            table.remove(adapter.data,d)
            table.insert(adapter.data,d+1,data)
            adapter.notifyDataSetChanged()
           else
            print("最下边了")
          end
        end
        if str=="延时"
          AlertDialog.Builder(this) .setTitle("请输入延时") .setView(loadlayout({EditText,id="et";hint="单位为秒"})).setPositiveButton("确定", {onClick=function()
              adapter.data[d].delay=et.text.."秒"
              adapter.notifyDataSetChanged()
            end}).setNegativeButton("取消", nil).show()
        end
      end}).show()
  end
end
avMix.background=btBackground
avMix. onClick=function()
  local i="-i "..iDir.text.." "
  local filter_complex=""
  local tag=""
  for n=1,#adapter.data-1 do
    i=i.. "-i "..adapter.data[n].dir.." "
    local seconds=(adapter.data[n].delay):gsub("秒","")
    if filter_complex==""
      filter_complex=filter_complex.. "["..tostring(n).."]adelay=".. tostring(tonumber(seconds)*1000).."|"..tostring(tonumber(seconds)*1000).."[tag".. tostring(n).."]"
     else
      filter_complex=filter_complex.. ",["..tostring(n).."]adelay=".. tostring(tonumber(seconds)*1000).."|"..tostring(tonumber(seconds)*1000).."[tag".. tostring(n).."]"
    end
    tag=tag.."[tag".. tostring(n).."]"
  end
  tag=tag.. "amix=".. tostring(#adapter.data-1)
  i=i.."-filter_complex "..filter_complex..","..tag.." -codec:v copy -y "
  local s=tostring(File(iDir.text).getName())
  s=s:sub(s:find("%."),#s)
  i=i..oDir.text..filename.text..s
  activity.newActivity("handle",{i})
end